home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / euphor12 / refman.doc < prev    next >
Text File  |  1994-07-07  |  80KB  |  2,042 lines

  1.  
  2.         Euphoria Programming Language 
  3.             version 1.2
  4.               Reference Manual
  5.  
  6.  
  7.         (c) 1994 Rapid Deployment Software
  8.     
  9.         Permission is freely granted to anyone
  10.         to copy this manual.
  11.  
  12.  
  13.  
  14.  
  15.             TABLE OF CONTENTS
  16.             =================
  17.  
  18.     1. Introduction 
  19.       
  20.        1.1  Example Program                  
  21.        1.2  Installation             
  22.        1.3  Running a Program    
  23.        1.4  Editing a Program   
  24.        1.5  Distributing a Program
  25.     
  26.     2. Core Language        
  27.      
  28.        2.1  Objects                 
  29.        2.2  Expressions     
  30.        2.3  Declarations 
  31.        2.4  Statements      
  32.        2.5  Top-Level Commands 
  33.     
  34.     3. Debugging 
  35.     
  36.     4. Built-in Routines       
  37.       
  38.        4.1  Predefined Types         
  39.        4.2  Sequence Manipulation   
  40.        4.3  Searching and Sorting  
  41.        4.4  Math            
  42.        4.5  File and Device I/O 
  43.        4.6  Mouse Support   
  44.        4.7  Operating System 
  45.        4.8  Debugging       
  46.        4.9  Graphics & Sound 
  47.        4.10 Machine Level Interface 
  48.  
  49.  
  50.  
  51.     
  52.             1. Introduction
  53.             ===============
  54.         
  55.  Euphoria is a new programming language with the following advantages over 
  56.  conventional languages:
  57.  
  58.  o      a remarkably simple, flexible, powerful language
  59.     definition that is extremely easy to learn and use.  
  60.  
  61.  o      dynamic storage allocation. Variables grow or shrink
  62.     without the programmer having to worry about allocating
  63.     and freeing chunks of memory.  Objects of any size can be 
  64.     assigned to an element of a Euphoria sequence (array).
  65.  
  66.  o      a high-performance, state-of-the-art interpreter that is
  67.     10 to 20 times faster than conventional interpreters such as
  68.     Microsoft QBasic.  
  69.  
  70.  o      lightning-fast pre-compilation. Your program is checked
  71.     for syntax and converted into an efficient internal form at
  72.     over 12,000 lines per second on a 486-50. 
  73.  
  74.  o      extensive run-time checking for: out-of-bounds subscripts,
  75.     uninitialized variables, bad parameter values for built-in
  76.     functions, illegal value assigned to a variable and many 
  77.     more.  There are no mysterious machine exceptions -- you 
  78.     will always get a full English description of any problem 
  79.     that occurs with your program at run-time, along with a 
  80.     call-stack trace-back and a dump of all of your variable
  81.     values.  Programs can be debugged quickly, easily and
  82.     more thoroughly.
  83.  
  84.  o      features of the underlying hardware are completely hidden. 
  85.     Programs are not aware of word-lengths,    underlying bit-level 
  86.     representation of values, byte-order etc. Euphoria programs are 
  87.     therefore highly portable from one machine to another.
  88.  
  89.  o      a full-screen source debugger and an execution profiler
  90.     are included, along with a full-screen editor. On a color 
  91.     monitor, the editor displays Euphoria programs in 
  92.     multiple colors, to highlight comments, reserved words, 
  93.     built-in functions, strings, and level of nesting of brackets. 
  94.     It optionally performs auto-completion of statements, 
  95.     saving you typing effort and reducing syntax errors. This 
  96.     editor is written in Euphoria, and the source code is 
  97.     provided to you without restrictions. You are free to
  98.     modify it, add features, and redistribute it as you wish. 
  99.  
  100.  o      Euphoria programs run under MS-DOS (or Windows), but are not
  101.     subject to any 64K or 640K memory limitations. You can
  102.     create programs that use the full multi-megabyte memory
  103.     of your computer. You can even set up a swap file for
  104.     programs that need more memory than exists on your machine. 
  105.     A least-recently-used paging algorithm will automatically 
  106.     shuffle pages of virtual memory in and out as your program 
  107.     executes.
  108.  
  109.  o      Euphoria routines are naturally generic. The example
  110.     program below shows a single routine that will sort any
  111.     type of data -- integers, floating-point numbers, strings
  112.     etc. Euphoria is not an "Object-Oriented" language in the 
  113.     usual sense, yet it achieves many of the benefits of these
  114.     languages in a much simpler way. 
  115.  
  116.  
  117.  1.1 Example Program 
  118.  -------------------
  119.  
  120.  The following is an example of a complete Euphoria program.
  121.  
  122.  
  123. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  124.  
  125.  sequence list, sorted_list
  126.  
  127.  function merge_sort(sequence x)
  128.  -- put x into ascending order using a recursive merge sort
  129.      integer n, mid
  130.      sequence merged, x1, x2
  131.  
  132.      n = length(x)
  133.      if n = 0 or n = 1 then
  134.      return x  -- trivial case
  135.      end if
  136.  
  137.      mid = floor(n/2)
  138.      x1 = merge_sort(x[1..mid])       -- sort first half of x 
  139.      x2 = merge_sort(x[mid+1..n])     -- sort second half of x
  140.  
  141.      -- merge the two sorted halves into one
  142.      merged = {}
  143.      while length(x1) > 0 and length(x2) > 0 do
  144.      if compare(x1[1], x2[1]) < 0 then
  145.          merged = append(merged, x1[1])
  146.          x1 = x1[2..length(x1)]
  147.      else
  148.          merged = append(merged, x2[1])
  149.          x2 = x2[2..length(x2)]
  150.      end if
  151.      end while
  152.      return merged & x1 & x2  -- merged data plus leftovers
  153.  end function
  154.  
  155.  procedure print_sorted_list()
  156.  -- generate sorted_list from list 
  157.      list = {9, 10, 3, 1, 4, 5, 8, 7, 6, 2}
  158.      sorted_list = merge_sort(list)
  159.      ? sorted_list   
  160.  end procedure
  161.  
  162.  print_sorted_list()     -- this command starts the program 
  163.  
  164.  
  165. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166.  
  167.  
  168.  The above example contains 4 separate commands that are processed in order.
  169.  The first declares two variables: list and sorted_list to be sequences. 
  170.  The second defines a function merge_sort(). The third defines a procedure 
  171.  print_sorted_list(). The final command calls procedure print_sorted_list().
  172.  
  173.  The output from the program will be:
  174.   {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}. 
  175.  
  176.  merge_sort() will just as easily sort {1.5, -9, 1e6, 100} or
  177.  {"oranges", "apples", "bananas"} .
  178.  
  179.  This example is stored as euphoria\demo\example.ex. This is not the fastest 
  180.  way to sort in Euphoria. Go into the euphoria\demo directory and type 
  181.  "ex allsorts" to see timings on several different sorting algorithms for 
  182.  increasing numbers of objects. For a quick tutorial on Euphoria programming
  183.  see euphoria\demo\bench\filesort.ex.
  184.  
  185.  
  186.  1.2 Installation 
  187.  ----------------
  188.  
  189.  To install Euphoria on your machine, first read the file install.doc. 
  190.  Installation simply involves copying the euphoria files to your hard disk 
  191.  under a directory named "EUPHORIA", and then modifying your autoexec.bat file
  192.  so that EUPHORIA\BIN is on your search path, and the environment variable 
  193.  EUDIR is set to the EUPHORIA directory. An automatic install program, 
  194.  "install.bat" is provided for this purpose. For the latest details, please 
  195.  read the instructions in install.doc before you run install.bat.
  196.  
  197.  When installed, the euphoria directory will look something like this: 
  198.  
  199.     euphoria 
  200.         readme.doc
  201.         \bin        
  202.             ex.exe, dos4gw.exe, ed.bat, other utilities
  203.         \include
  204.             standard include files, e.g. graphics.e
  205.         \doc
  206.             ed.doc, refman.doc etc.
  207.         \demo   
  208.             demo programs, e.g. ttt.ex, mset.ex, plot3d.ex
  209.             \learn
  210.                test your knowledge of Euphoria
  211.             \langwar
  212.                language war game, lw.ex
  213.             \bench  
  214.                benchmark programs
  215.            
  216.  
  217.  1.3 Running a Program
  218.  ------------------------
  219.  
  220.  Euphoria programs are executed by typing "ex", followed by the name of the 
  221.  main (or only) file. By convention, main Euphoria files have an extension of 
  222.  ".ex".  Other Euphoria files, that are meant to be included in a larger
  223.  program, end in ".e". To save typing, you can leave off the ".ex", and 
  224.  the ex command will supply it for you automatically. If the file can't be 
  225.  found in the current directory, your PATH will be searched. There are no 
  226.  command-line options for ex itself, but your program can call the built-in 
  227.  function command_line() to read the ex command-line. You can redirect 
  228.  standard input and standard output when you run a Euphoria program, 
  229.  for example:
  230.  
  231.     ex filesort.ex < raw > sorted
  232.  
  233.        or simply,
  234.  
  235.     ex filesort < raw > sorted
  236.  
  237.  For frequently-used programs you might want to make a small .bat file 
  238.  containing something like:
  239.      
  240.       @echo off
  241.       ex myprog.ex %1 %2
  242.  
  243.  where myprog.ex expects two command-line arguments. This will save you 
  244.  from typing ex all the time. 
  245.  
  246.  ex.exe is in the euphoria\bin directory which must be on your search path. 
  247.  The file dos4gw.exe must also be present in the bin directory (or somewhere 
  248.  on the search path). Some Euphoria programs expect the environment variable 
  249.  EUDIR to be set to the main Euphoria directory.
  250.  
  251.  Running Under Windows
  252.  ---------------------
  253.  You can run Euphoria programs directly from the Windows environment, or from 
  254.  a DOS shell that you have opened from Windows. By "associating" .ex files
  255.  with ex.exe, you can simply double-click on a .ex file to run it. It
  256.  is possible to have several Euphoria programs active in different windows.
  257.  You can resize these windows, move them around, change to a different font, 
  258.  run things in the background, copy and paste between windows etc. See your 
  259.  Windows manual. The Euphoria editor is available. You might want to 
  260.  associate .e, .pro and other text files with ed.bat. Also, the File-menu/
  261.  Run-command will let you type in ex or ed command lines. 
  262.  
  263.  Use of a swap file
  264.  ------------------
  265.  If your program requires more memory than is physically present on your 
  266.  machine, you can easily set up a swap file to provide extra "virtual" 
  267.  memory under DOS. All you have to do is define the environment variable 
  268.  DOS4GVM. The MS-DOS command:  set DOS4GVM=1  will allow a 16 megabyte swap 
  269.  file to be created, which will be used as virtual memory. Portions of your 
  270.  program and data that have not been recently used will be copied out to this 
  271.  file to create room in physical memory for actively-used information. If you
  272.  can't afford the default 16 megabytes of disk space, you could: 
  273.  set DOS4GVM=virtualsize#8192  to get an 8 megabyte swap file, or specify a 
  274.  smaller number if this is still too much. To turn off virtual memory, 
  275.  you can:  set DOS4GVM=  after your program has finished executing. The swap 
  276.  file can be deleted after execution, but leaving it in place will let your 
  277.  next program start up faster.  
  278.  
  279.  Before allowing the swap file, \dos4gvm.swp, to be created, make sure that 
  280.  you have enough disk space for it, plus a few megabytes extra for breathing 
  281.  room. The dos4gvm.swp file is peculiar in that the actual amount of disk
  282.  space allocated to the file may initially be much less than the file size 
  283.  reported by the DOS dir command. You could see error messages when the actual
  284.  allocated space approaches the reported size, and your disk runs low on free
  285.  space.  
  286.  
  287.  If your machine has more than 4 megabytes of memory, you should specify 
  288.  the "maxmem" parameter to increase the amount of memory controlled
  289.  by the virtual memory manager, e.g.
  290.  
  291.     set DOS4GVM=maxmem#8192
  292.  
  293.  would let the virtual memory manager control up to 8 megabytes of RAM.
  294.  The default for "maxmem" is 4096, i.e. 4 megabytes. You would still get
  295.  a 16 megabyte swap file by default. In general you can specify values for
  296.  both virtualsize and maxmem, e.g.
  297.  
  298.     set DOS4GVM=virtualsize#12000 maxmem#6000
  299.  
  300.  When you run under Windows, virtual memory swapping will be performed 
  301.  by Windows itself using its own swap file, and you will typically get more 
  302.  memory than DOS (without dos4gvm.swp) can provide. To review or change the
  303.  size of the Windows swap file click on Control Panel / 386 Enhanced /
  304.  "virtual memory...".
  305.  
  306.  
  307.  1.4 Editing a Program 
  308.  ---------------------
  309.  
  310.  You can use any text editor to edit a Euphoria program. However, Euphoria 
  311.  comes with its own special editor which is written entirely in Euphoria. 
  312.  Type ed followed by the complete name of the file you wish to edit (the 
  313.  .ex extension is not assumed). You can use this editor to edit any kind of 
  314.  text file. When you edit a .e or .ex file some extra features, such as color 
  315.  syntax highlighting and auto-completion of certain statements, are available 
  316.  to make your job easier.
  317.  
  318.  Whenever you run a Euphoria program and get an error message, during 
  319.  compilation or execution, you can simply type ed with no file name and you 
  320.  will be automatically positioned in the file containing the error, at 
  321.  the correct line and column, and with the error message displayed at the 
  322.  top of the screen. 
  323.  
  324.  Under Windows you can associate ed.bat with various kinds of text files 
  325.  that you want to edit.
  326.  
  327.  Most keys that you type are inserted into the file at the cursor position. 
  328.  Hit the Esc key once to get a menu bar of special commands. The arrow keys, 
  329.  and the Insert/Delete Home/End PageUp/PageDown keys are also active. See 
  330.  the file euphoria\doc\ed.doc for a complete description of the editing 
  331.  commands. Esc h (help) will let you view ed.doc from your editing session.
  332.  
  333.  If you need to understand or modify any detail of the editor's operation, 
  334.  you can edit the file ed.ex in euphoria\bin (be sure to make a backup 
  335.  copy so you don't lose your ability to edit).  If the name ed conflicts 
  336.  with some other command on your system, simply rename the file 
  337.  euphoria\bin\ed.bat to something else.  Because this editor is written 
  338.  in Euphoria, it is remarkably concise and easy to understand. The same 
  339.  functionality implemented in a language like C, would take far more 
  340.  lines of code.
  341.  
  342.  
  343.  1.5 Distributing a Program
  344.  --------------------------
  345.  
  346.  Your customer needs to have the 2 files: ex.exe and dos4gw.exe somewhere 
  347.  on the search path. You are free to supply anyone with the Public Domain
  348.  Edition of ex.exe, as well as dos4gw.exe to support it.
  349.  
  350.  Your program can be distributed in source form or in shrouded form. In source
  351.  form you supply your Euphoria files plus any standard include files that are
  352.  required. To deliver a program in shrouded form, you run the Euphoria source 
  353.  code shrouder, bin\shroud.ex, against your main Euphoria file. The shrouder 
  354.  pulls together all included files into a single compact file that is 
  355.  virtually unreadable. You then ship this one file plus a copy of ex.exe and 
  356.  dos4gw.exe. One copy of ex.exe and dos4gw.exe on a machine is sufficient to 
  357.  run any number of Euphoria programs. Comments in bin\shroud.ex tell you how 
  358.  to run it, and what it does to obscure or "shroud" your source. 
  359.  
  360.  
  361.  
  362.             2. The Core Language
  363.             ====================
  364.  
  365.  2.1 Objects
  366.  -----------
  367.  
  368.  All data objects in Euphoria are either atoms or sequences.  An atom is a 
  369.  single numeric value. A sequence is an ordered list of data objects.  
  370.  The objects contained in a sequence can be an arbitrary mix of atoms or 
  371.  sequences. A sequence is represented by a list of objects in brace brackets,
  372.  separated by commas. Atoms can have any double-precision floating point value.
  373.  This is approximately -1e300 to +1e300 with 15 decimal digits of accuracy.
  374.  Here are some Euphoria objects:
  375.  
  376.     -- examples of atoms:
  377.     0
  378.     1000
  379.     98.6
  380.     -1e6
  381.  
  382.     -- examples of sequences:
  383.     {2, 3, 5, 7, 11, 13, 17, 19}        -- 8-element sequence
  384.     {1, 2, {3, 3, 3}, 4, {5, {6}}}      -- 5-element sequence
  385.     {{"jon", "smith"}, 52389, 97.25}    -- 3-element sequence
  386.     {}                                  -- 0-element sequence
  387.  
  388.  Numbers can also be entered in hexadecimal. For example:
  389.     #FE             -- 254
  390.     #A000           -- 40960
  391.     #FFFF00008      -- 68718428168
  392.     -#10            -- -16
  393.  
  394.  Sequences can be nested to any depth.  Brace brackets are used to construct 
  395.  sequences out of a list of expressions.  These expressions are evaluated at 
  396.  run-time. e.g.
  397.  
  398.     {x+6, 9, y*w+2, sin(0.5)}
  399.  
  400.  The "Hierarchical Objects" part of the Euphoria acronym comes from the
  401.  hierarchical nature of nested sequences. This should not be confused with
  402.  the class hierarchies of certain object-oriented languages. 
  403.  
  404.  Performance Note: Although atoms can have any double-precision value,
  405.  integer-valued atoms are generally stored and manipulated as machine integers
  406.  to save time and space. 
  407.  
  408.  
  409.  Character Strings
  410.  -----------------
  411.  Character strings may be entered using quotes e.g.
  412.  
  413.     "ABCDEFG"
  414.  
  415.  Strings are just sequences of characters, and may be manipulated and 
  416.  operated upon just like any other sequences. For example the above 
  417.  string is equivalent to the sequence
  418.  
  419.     {65, 66, 67, 68, 69, 70, 71}
  420.  
  421.  which contains the corresponding ASCII codes. Individual characters may be 
  422.  entered using single quotes if it is desired that they be treated as 
  423.  individual numbers (atoms) and not length-1 sequences. e.g.
  424.  
  425.      'B'   -- equivalent to the atom 66
  426.      "B"   -- equivalent to the sequence {66}
  427.  
  428.  Note that an atom is not equivalent to a one-element sequence containing 
  429.  the same value, although there are a few built-in routines that choose 
  430.  to treat them similarly.
  431.  
  432.  Special characters may be entered using a back-slash:
  433.  
  434.     \n        newline
  435.     \r        carriage return
  436.     \t        tab
  437.     \\        backslash
  438.     \"        double quote
  439.     \'        single quote
  440.  
  441.  For example, "Hello, World!\n", or '\\'. The Euphoria editor displays 
  442.  character strings in brown. 
  443.  
  444.  Comments
  445.  --------
  446.  Comments are started by two dashes and extend to the end of the current line.
  447.  e.g.
  448.  
  449.      -- this is a comment
  450.  
  451.  Comments are ignored by the compiler and have no effect on execution speed. 
  452.  The editor displays comments in red. In this manual we use italics.
  453.  
  454.  
  455.  2.2 Expressions
  456.  ---------------
  457.  
  458.  Objects can be combined into expressions using binary and unary operators as 
  459.  well as built-in and user-defined functions. For example,
  460.  
  461.     {1,2,3} + 5
  462.  
  463.  is an expression that adds the sequence {1,2,3} and the atom 5 to get the 
  464.  resulting sequence {6,7,8}. Besides + there are many other operators. The 
  465.  precedence of operators is as follows:
  466.  
  467.     highest precedence:     function/type calls
  468.  
  469.                 unary -  not 
  470.  
  471.                 *  /
  472.  
  473.                 +  -
  474.  
  475.                 &
  476.  
  477.                 <  >  <=  >=  =  !=
  478.  
  479.      lowest precedence:     and, or
  480.  
  481.  Thus 2+6*3 means 2+(6*3), not (2+6)*3. Operators on the same line above have 
  482.  equal precedence and are evaluated left to right.
  483.  
  484.  
  485.  Relational & Logical Operators
  486.  ------------------------------
  487.  The relational operators, <, >, <=, >=, = , != each produce a 1 (true) or a 
  488.  0 (false) result. These results can be used by the logical operators 'and', 
  489.  'or', and 'not' to determine an overall truth value. e.g.
  490.  
  491.     b > 0 and b != 100 or not (c <= 5) 
  492.  
  493.  where b and c are the names of variables.  
  494.  
  495.  
  496.  Subscripting of Sequences
  497.  -------------------------
  498.  A single element of a sequence may be selected by giving the element number 
  499.  in square brackets. Element numbers start at 1. Non-integer subscripts are 
  500.  rounded down to an integer. 
  501.  
  502.  For example, if x contains {5, 7, 9, 11, 13} then x[2] is 7. Suppose we 
  503.  assign something different to x[2]:
  504.  
  505.     x[2] = {11,22,33}
  506.  
  507.  Then x becomes: {5, {11,22,33}, 9, 11, 13}. Now if we ask for x[2] we get 
  508.  {11,22,33} and if we ask for x[2][3] we get the atom 33. If you try to 
  509.  subscript with a number that is outside of the range 1 to the number of 
  510.  elements, you will get a subscript error. For example x[0],  x[-99] or 
  511.  x[6] will cause errors. So will x[1][3] since x[1] is not a sequence. There 
  512.  is no limit to the number of subscripts that may follow a variable, but 
  513.  the variable must contain sequences that are nested deeply enough. The 
  514.  two dimensional array, common in other languages, can be easily simulated 
  515.  with a sequence of sequences:
  516.  
  517.      { {5, 6, 7, 8, 9},
  518.        {1, 2, 3, 4, 5},
  519.        {0, 1, 0, 1, 0} }
  520.  
  521.  An expression of the form x[i][j] can be used to access any element. The two 
  522.  dimensions are not symmetric however, since an entire "row" can be selected 
  523.  with x[i], but there is no simple expression to select an entire column. 
  524.  Other logical structures, such as n-dimensional arrays, arrays of strings, 
  525.  arrays of structures etc. can also be handled easily and flexibly.
  526.     
  527.  Note that expressions in general may not be subscripted, just variables. For 
  528.  example: {5,6,7,8}[3] is not supported. 
  529.  
  530.  
  531.  Slicing of Sequences
  532.  --------------------
  533.  A sequence of consecutive elements may be selected by giving the starting and 
  534.  ending element numbers. For example if x is {1, 1, 2, 2, 2, 1, 1, 1} then 
  535.  x[3..5] is the sequence {2, 2, 2}. x[3..3] is the sequence {2}. x[3..2] is 
  536.  also allowed. It evaluates to the length-0 sequence {}.  If y has the value: 
  537.  {"fred", "george", "mary"} then y[1..2] is {"fred", "george"}.
  538.  
  539.  We can also use slices for overwriting portions of variables. After x[3..5] = 
  540.  {9, 9, 9} x would be {1, 1, 9, 9, 9, 1, 1, 1}. We could also have said 
  541.  x[3..5] = 9 with the same effect. Suppose y is {0, "Euphoria", 1, 1}. 
  542.  Then y[2][1..4] is "Euph". If we say y[2][1..4]="ABCD" then y will 
  543.  become {0, "ABCDoria", 1, 1}.
  544.  
  545.  We need to be a bit more precise in defining the rules for empty slices. 
  546.  Consider a slice s[i..j] where s is of length n. A slice from i to j, 
  547.  where  j = i-1  and i >= 1 produces the empty sequence, even if i = n+1. 
  548.  Thus 1..0  and n+1..n and everything in between are legal (empty) slices. 
  549.  Empty slices are quite useful in many algorithms. A slice from i to j where 
  550.  j < i - 1 is illegal , i.e. "reverse" slices such as s[5..3] are not allowed. 
  551.  
  552.  
  553.  Concatenation of Sequences and Atoms
  554.  ------------------------------------
  555.  Any two objects may be concatenated using the & operator. The result is a 
  556.  sequence with a length equal to the sum of the lengths of the concatenated 
  557.  objects (where atoms are considered here to have length 1). e.g.
  558.  
  559.     {1, 2, 3} & 4   -- result is {1, 2, 3, 4}
  560.  
  561.     4 & 5           -- result is {4, 5}
  562.  
  563.     {{1, 1}, 2, 3} & {4, 5}   -- result is {{1, 1}, 2, 3, 4, 5}
  564.  
  565.     x = {}  
  566.     y = {1, 2}
  567.     y = y & x       -- y is still {1, 2}
  568.  
  569.  
  570.  Arithmetic Operations on Sequences
  571.  ----------------------------------
  572.  Any binary or unary arithmetic operation, including any of the built-in 
  573.  math routines, may be applied to entire sequences as well as to single 
  574.  numbers.
  575.  
  576.  When applied to a sequence, a unary operator is actually applied to each 
  577.  element in the sequence to yield a sequence of results of the same length. 
  578.  If one of these elements is itself a sequence then the same rule is applied 
  579.  recursively. e.g.
  580.  
  581.     x = -{1, 2, 3, {4, 5}}   -- x is {-1, -2, -3, {-4, -5}}
  582.  
  583.  If a binary operator has operands which are both sequences then the two 
  584.  sequences must be of the same length. The binary operation is then applied 
  585.  to corresponding elements taken from the two sequences to get a sequence of 
  586.  results. e.g.
  587.  
  588.     x = {5, 6, 7 {1, 1}} + {10, 10, 20, 100}
  589.     -- x is {15, 16, 27, {101, 101}}
  590.  
  591.  If a binary operator has one operand which is a sequence while the other is a 
  592.  single number (atom) then the single number is effectively repeated to 
  593.  form a sequence of equal length to the sequence operand. The rules for 
  594.  operating on two sequences then apply. Some examples:
  595.  
  596.     y = {4, 5, 6}
  597.  
  598.     w = 5 * y  -- w is {20, 25, 30}
  599.  
  600.     x = {1, 2, 3}
  601.     
  602.     z = x + y  -- z is {5, 7, 9}
  603.  
  604.     z = x < y  -- z is {1, 1, 1}
  605.  
  606.     w = {{1, 2}, {3, 4}, {5}}
  607.    
  608.     w = w * y  -- w is {{4, 8}, {15, 20}, {30}}
  609.  
  610.  
  611.  Comparison of Euphoria Objects with Other Languages
  612.  ---------------------------------------------------
  613.  By basing Euphoria on this one, simple, general, recursive data structure, 
  614.  a tremendous amount of the complexity normally found in programming languages
  615.  has been avoided. The arrays, record structures, unions, arrays of records, 
  616.  multidimensional arrays, etc. of other languages can all be easily 
  617.  simulated in Euphoria with sequences. So can higher-level structures such
  618.  as lists, stacks, queues, trees etc. 
  619.  
  620.  Furthermore, in Euphoria you can have sequences of mixed type; you can 
  621.  assign any object to an element of a sequence; and sequences easily grow or 
  622.  shrink in length without your having to worry about storage allocation issues.
  623.  The exact layout of a data structure does not have to be declared in advance,
  624.  and can change dynamically as required. It is easy to write generic code,
  625.  where, for instance, you push or pop a mix of various kinds of data 
  626.  objects using a single stack. 
  627.  
  628.  Data structure manipulations are very efficient since Euphoria will point to 
  629.  large data objects rather than copy them. 
  630.  
  631.  Programming in Euphoria is based entirely on creating and manipulating 
  632.  flexible, dynamic sequences of data. Sequences are it - there are no
  633.  other data structures to learn. You operate in a simple, safe, elastic world 
  634.  of *values*, that is far removed from the rigid, tedious, dangerous world
  635.  of bits, bytes, pointers and machine crashes. 
  636.  
  637.  Unlike other languages such as LISP and Smalltalk, Euphoria's 
  638.  "garbage collection" of unused storage is a continuous process that never 
  639.  causes random delays in execution of a program, and does not pre-allocate 
  640.  huge regions of memory.   
  641.  
  642.  The language definitions of conventional languages such as C, C++, Ada, etc.
  643.  are very complex. Most programmers become fluent in only a subset of the 
  644.  language. The ANSI standards for these languages read like complex legal 
  645.  documents. 
  646.  
  647.  You are forced to write different code for different data types simply to 
  648.  copy the data, ask for its current length, concatenate it, compare it etc. 
  649.  The manuals for those languages are packed with routines such as "strcpy",
  650.  "strncpy", "memcpy", "strcat",  "strlen", "strcmp", "memcmp", etc. that 
  651.  each only work on one of the many types of data.
  652.  
  653.  Much of the complexity surrounds issues of data type. How do you define 
  654.  new types? Which types of data can be mixed? How do you convert one type 
  655.  into another in a way that will keep the compiler happy? When you need to 
  656.  do something requiring flexibility at runtime, you frequently find yourself 
  657.  trying to fake out the compiler.
  658.  
  659.  In these languages the numeric value 4 (for example) can have a different 
  660.  meaning depending on whether it is an int, a char, a short, a double, an  
  661.  int * etc.. In Euphoria, 4 is the atom 4, period. Euphoria has something 
  662.  called types as we shall see later, but it is a much simpler concept.
  663.  
  664.  Issues of dynamic storage allocation and deallocation consume a great deal 
  665.  of programmer coding time and debugging time in these other languages, and 
  666.  make the resulting programs much harder to understand. 
  667.  
  668.  Pointer variables are extensively used. The pointer has been called the 
  669.  "go to" of data structures. It forces programmers to think of data as 
  670.  being bound to a fixed memory location where it can be manipulated in all 
  671.  sorts of low-level non-portable, tricky ways. A picture of the actual 
  672.  hardware that your program will run on is never far from your mind. Euphoria
  673.  does not have pointers and does not need them.
  674.  
  675.  
  676.  2.3 Declarations
  677.  ----------------
  678.  
  679.  Identifiers
  680.  -----------
  681.  Variable names and other user-defined symbols (identifiers) may be of any 
  682.  length. Upper and lower case are distinct. Identifiers must start with a 
  683.  letter and then be followed by letters, digits or underscores. The 
  684.  following reserved words have special meaning in Euphoria and may not be 
  685.  used as identifiers:
  686.  
  687.  and            end             include         then
  688.  by             exit            not             to
  689.  constant       for             or              type
  690.  do             function        procedure       while
  691.  else           global          profile         with
  692.  elsif          if              return          without 
  693.  
  694.  The Euphoria editor displays these words in blue. In this manual we use 
  695.  boldface.
  696.  
  697.  The following kinds of user-defined symbols may be declared in a program:
  698.  
  699.      o  procedures 
  700.     These perform some computation and may have a list of parameters, 
  701.     e.g.
  702.    
  703.     procedure empty()
  704.     end procedure
  705.  
  706.     procedure plot(integer x, integer y)
  707.         position(x, y)
  708.         puts(1, '*')
  709.     end procedure
  710.     
  711.     There are a fixed number of named parameters, but this is not 
  712.     restrictive since any parameter could be a variable-length sequence 
  713.     of arbitrary objects. In many languages variable-length parameter 
  714.     lists are impossible.  In C, you must set up strange mechanisms that
  715.     are complex enough that the average programmer cannot do it without
  716.     consulting a manual or a local guru. 
  717.  
  718.     A copy of the value of each argument is passed in. The formal 
  719.     parameter variables may be modified inside the procedure but this does
  720.     not affect the value of the arguments.
  721.  
  722.     Performance Note: The interpreter does not copy sequences unless it 
  723.     becomes necessary. For example,
  724.         y = {1,2,3,4,5,6,7}
  725.         x = y
  726.     The statement x = y does not cause the value of y to be copied.
  727.     Both x and y will simply "point" to the same data. If we later perform
  728.     x[3] = 9, then x will be given its own separate copy. The same thing 
  729.     applies to "copies" of arguments passed in to subroutines.
  730.     
  731.      o  functions 
  732.     These are just like procedures, but they return a value, and can be
  733.     used in an expression, e.g.
  734.  
  735.     function max(atom a, atom b)
  736.         if a >= b then
  737.         return a
  738.         else
  739.         return b
  740.         end if
  741.     end function
  742.  
  743.     Any Euphoria object can be returned.  You can, in effect, have 
  744.     multiple return values, by returning a sequence of objects. e.g.
  745.     
  746.     return {quotient, remainder}
  747.  
  748.     We will use the general term "subroutine", or simply "routine" when a
  749.     remark is applicable to both procedures and functions.
  750.  
  751.      o  types 
  752.     These are special functions that may be used in declaring the allowed
  753.     values for a variable. A type must have exactly one parameter and 
  754.     should return an atom that is either TRUE (non-zero) or FALSE (zero).
  755.     Types can also be called just like other functions. They are discussed
  756.     in more detail below.
  757.  
  758.      o  variables 
  759.     These may be assigned values during execution e.g.
  760.  
  761.     integer x
  762.     x = 25
  763.  
  764.     object a, b, c
  765.     a = {}
  766.     b = a
  767.     c = 0
  768.  
  769.      o  constants
  770.     These are variables that are assigned an initial value that can 
  771.     never change e.g.
  772.       
  773.     constant MAX = 100
  774.     constant upper = MAX - 10, lower = 5
  775.  
  776.     The result of any expression can be assigned to a constant,
  777.     even one involving calls to previously defined functions, but once
  778.     the assignment is made the value of the constant variable is 
  779.     "locked in".
  780.  
  781.  
  782.  Scope
  783.  -----
  784.  Every symbol must be declared before it is used. This is restrictive, but it
  785.  has benefits. It means you always know in which direction to look for the 
  786.  definition of a subroutine or variable that is used at some point in the 
  787.  program. When looking at a subroutine definition, you know that there could 
  788.  not be a call to this routine from any routine defined earlier. In general, 
  789.  it forces you to organize your program into a hierarchy where there are 
  790.  distinct, "layers" of low-level, followed by higher-level routines. You 
  791.  can replace a layer without disrupting any lower layers.
  792.  
  793.  A symbol is defined from the point where it is declared to the end of its 
  794.  scope. The scope of a variable declared inside a procedure or function (a 
  795.  private variable) ends at the end of the procedure or function.  The scope 
  796.  of all other constants, procedures, functions and variables ends at the end
  797.  of the source file in which they are declared and they are referred to as 
  798.  local, unless the word global precedes their declaration, in which case their 
  799.  scope extends indefinitely. Procedures and functions can call themselves 
  800.  recursively.
  801.  
  802.  Constant declarations must be outside of any subroutine.
  803.  
  804.  Variable declarations inside a subroutine must all appear at the beginning,
  805.  before the executable statements of the subroutine.
  806.  
  807.  A special case is that of the controlling variable used in a for-loop. It is 
  808.  automatically declared at the beginning of the loop, and its scope ends at 
  809.  the end of the for loop. If the loop is inside a function or procedure, the 
  810.  loop variable is a private variable and may not have the same name as any 
  811.  other private variable. When the loop is at the top level, outside of any  
  812.  function or procedure, the loop variable is a local variable and may not have 
  813.  the same name as any other global or local variable in that file. You do not 
  814.  declare loop variables as you would other variables. The range of values 
  815.  specified in the for statement defines the legal values of the loop variable 
  816.  - specifying a type would be redundant and is not allowed.
  817.  
  818.  
  819.  Specifying the type of a variable
  820.  ---------------------------------
  821.  
  822.  Variable declarations have a type name followed by a list of the variables 
  823.  being declared. For example,
  824.  
  825.     object a 
  826.  
  827.     global integer x, y, z 
  828.  
  829.     procedure fred(sequence q, sequence r)
  830.  
  831.  In a parameter list like the one above, the type name may only be followed by 
  832.  a single variable name. 
  833.  
  834.  The types: object, sequence, atom and integer are predefined. Variables of 
  835.  type object may take on any value. Those declared with type sequence must  
  836.  always be sequences. Those declared with type atom must always be atoms. Those
  837.  declared with type integer must be atoms with integer values from -1073741824 
  838.  to +1073741823 inclusive. You can perform exact calculations on larger integer
  839.  values, up to about 15 decimal digits, but declare them as atom, rather than 
  840.  integer.
  841.  
  842.  Performance Note: Calculations using integer variables will usually be 
  843.  somewhat faster than calculations involving atom variables. If your
  844.  machine has floating-point hardware, Euphoria will use it to manipulate  
  845.  atoms that aren't representable as integers, otherwise floating-point
  846.  emulation routines contained in ex.exe are used.
  847.  
  848.  To augment the predefined types, you can create new types. All you have to 
  849.  do is define a single-parameter function, but declare it with  
  850.  type ... end type instead of function ... end function. For example,
  851.  
  852.  
  853.     type hour(integer x)
  854.          return x >= 0 and x <= 23
  855.     end type
  856.  
  857.     hour h1, h2
  858.  
  859.  This guarantees that variables h1 and h2 can only be assigned integer values 
  860.  in the range 0 to 23 inclusive. After an assignment to h1 or h2 the 
  861.  interpreter will call "hour()", passing the new value.  The parameter x will 
  862.  first be checked to see if it is an integer. If it is, the return statement 
  863.  will be executed to test the value of x (i.e. the new value of h1 or h2). 
  864.  If "hour" returns true, execution continues normally. If "hour" returns false
  865.  then the program is aborted with a suitable diagnostic message. 
  866.  
  867.     procedure set_time(hour h)
  868.  
  869.  set_time() above can only be called with a reasonable value for parameter h.
  870.  
  871.  A variable's type will be checked after each assignment to the variable 
  872.  (except where the compiler can predetermine that a check will not be 
  873.  necessary), and the program will terminate immediately if the type function 
  874.  returns false.  Subroutine parameter types are checked when the subroutine 
  875.  is called. This checking guarantees that a variable can never have a value 
  876.  that does not belong to the type of that variable.
  877.  
  878.  Unlike other languages, the type of a variable does not affect any 
  879.  calculations on the variable. Only the value of the variable matters in an 
  880.  expression. The type just serves as an error check to prevent any "corruption"
  881.  of the variable. 
  882.  
  883.  Type checking can be turned off or on in between subroutines using the 
  884.  with type_check or without type_check commands. It is initially on by default.
  885.  
  886.  Note to Benchmarkers: When comparing the speed of Euphoria programs against 
  887.  programs written in other languages, you should specify  without type_check 
  888.  at the top of the file, unless the other language provides a comparable 
  889.  amount of run-time checking. This gives Euphoria permission to skip runtime 
  890.  type checks, thereby saving some execution time. All other checks are still
  891.  performed, e.g. subscript checking, uninitialized variable checking etc. 
  892.  Even when you turn off type checking, Euphoria reserves the right to make 
  893.  checks at strategic places, since this can actually allow it to run your 
  894.  program faster in many cases. So you may still get a type check failure 
  895.  even when you have turned off type checking. With or without type_check, 
  896.  you will never get a machine-level exception. You will always get a 
  897.  meaningful message from Euphoria when something goes wrong.
  898.  
  899.  Euphoria's method of defining types is much simpler than what you will find 
  900.  in other languages, yet Euphoria provides the programmer with greater 
  901.  flexibility in defining the legal values for a type of data. Any algorithm 
  902.  can be used to include or exclude values. You can even declare a variable 
  903.  to be of type object which will allow it to take on any value. Routines can 
  904.  be written to work with very specific types, or very general types.
  905.  
  906.  Strict type definitions can greatly aid the process of debugging.  Logic 
  907.  errors are caught close to their source and are not allowed to propagate in 
  908.  subtle ways through the rest of the program. Furthermore, it is much easier 
  909.  to reason about the misbehavior of a section of code when you are guaranteed 
  910.  that the variables involved always had a legal value, if not the desired 
  911.  value.
  912.  
  913.  Types also provide meaningful, machine-checkable documentation about your 
  914.  program, making it easier for you or others to understand your code at a 
  915.  later date. Combined with the subscript checking, uninitialized variable 
  916.  checking, and other checking that is always present, strict run-time type 
  917.  checking makes debugging much easier in Euphoria than in most other 
  918.  languages. It also increases the reliability of the final program since 
  919.  many latent bugs that would have survived the testing phase in other 
  920.  languages will have been caught by Euphoria.
  921.  
  922.  Anecdote 1: In porting a large C program to Euphoria, a number
  923.  of latent bugs were discovered. Although this C program was believed to be 
  924.  totally "correct", we found: a situation where an uninitialized variable 
  925.  was being read; a place where element number "-1" of an array was routinely 
  926.  written and read; and a situation where something was written just off the 
  927.  screen. These problems resulted in errors that weren't easily visible to a 
  928.  casual observer, so they had survived testing of the C code. 
  929.  
  930.  Anecdote 2: The Quick Sort algorithm presented on page 117 of Writing 
  931.  Efficient Programs by Jon Bentley has a subscript error! The algorithm will 
  932.  sometimes read the element just before the beginning of the array to be 
  933.  sorted, and will sometimes read the element just after the end of the array. 
  934.  Whatever garbage is read, the algorithm will still work - this is probably 
  935.  why the bug was never caught. But what if there isn't any (virtual) memory 
  936.  just before or just after the array? Bentley later modifies the algorithm 
  937.  such that this bug goes away -- but he presented this version as being 
  938.  correct. Even the experts need subscript checking!
  939.  
  940.  Performance Note: When typical user-defined types are used extensively, type 
  941.  checking adds only 20 to 40 percent to execution time. Leave it on unless 
  942.  you really need the extra speed. You might also consider turning it off for
  943.  just a few heavily-executed routines. Profiling can help with this decision.
  944.  
  945.  
  946.  2.4 Statements
  947.  --------------
  948.  
  949.  The following kinds of executable statements are available:
  950.  
  951.     o   assignment statement
  952.  
  953.     o   procedure call 
  954.  
  955.     o   if statement
  956.  
  957.     o   while statement
  958.  
  959.     o   for statement 
  960.  
  961.     o   return statement
  962.  
  963.     o   exit statement
  964.  
  965.  Semicolons are not used in Euphoria, but you are free to put as many 
  966.  statements as you like on one line, or to split a single statement across 
  967.  many lines. You may not split a statement in the middle of a variable name, 
  968.  string, number or keyword. 
  969.  
  970.  An assignment statement assigns the value of an expression to a simple 
  971.  variable, or to a subscript or slice of a variable. e.g. 
  972.     
  973.     x = a + b
  974.  
  975.     y[i] = y[i] + 1
  976.  
  977.     y[i..j] = {1, 2, 3}
  978.  
  979.  The previous value of the variable, or element(s) of the subscripted or 
  980.  sliced variable are discarded.  For example, suppose x was a 1000-element 
  981.  sequence that we had initialized with:
  982.  
  983.     object x
  984.  
  985.     x = repeat(0, 1000)  -- repeat 0, 1000 times
  986.  
  987.  and then later we assigned an atom to x with:
  988.  
  989.     x = 7
  990.  
  991.  This is perfectly legal since x is declared as an object. The previous value 
  992.  of x, namely the 1000-element sequence, would simply disappear. Actually, 
  993.  the space consumed by the 1000-element sequence will be automatically 
  994.  recycled due to Euphoria's dynamic storage allocation.
  995.  
  996.  A procedure call starts execution of a procedure, passing it an optional list 
  997.  of argument values. e.g.
  998.  
  999.     plot(x, 23)
  1000.  
  1001.  An if statement tests an expression to see if it is 0 (false) or non-zero 
  1002.  (true) and then executes the appropriate series of statements.  There may 
  1003.  be optional elsif and else clauses. e.g.
  1004.  
  1005.     if a < b then
  1006.         x = 1
  1007.     end if
  1008.  
  1009.  
  1010.     if a = 9 then
  1011.         x = 4
  1012.         y = 5
  1013.     else
  1014.         z = 8
  1015.     end if
  1016.  
  1017.  
  1018.     if char = 'a' then
  1019.         x = 1
  1020.     elsif char = 'b' then
  1021.         x = 2
  1022.     elsif char = 'c' then
  1023.         x = 3
  1024.     else
  1025.         x = -1
  1026.     end if
  1027.  
  1028.  A while statement tests an expression to see if it is non-zero (true), 
  1029.  and while it is true a loop is executed. e.g.
  1030.  
  1031.     while x > 0 do
  1032.         a = a * 2
  1033.         x = x - 1
  1034.     end while
  1035.  
  1036.  A for statement sets up a special loop with a controlling loop variable 
  1037.  that runs from an initial value up or down to some final value. e.g.
  1038.  
  1039.     for i = 1 to 10 do
  1040.         print(1, i)
  1041.     end for
  1042.  
  1043.     for i = 10 to 20 by 2 do
  1044.         for j = 20 to 10 by -2 do
  1045.         print(1, {i, j})
  1046.         end for
  1047.     end for
  1048.  
  1049.  The loop variable is declared automatically and exists until the end of the 
  1050.  loop. Outside of the loop the variable has no value and is not even declared.
  1051.  If you need its final value, copy it into another variable before leaving 
  1052.  the loop. The compiler will not allow any assignments to a loop variable. The
  1053.  initial value, loop limit and increment must all be atoms. If no increment 
  1054.  is specified then +1 is assumed. The limit and increment values are 
  1055.  established when the loop is entered, and are not affected by anything that 
  1056.  happens during the execution of the loop. 
  1057.  
  1058.  A return statement returns from a subroutine. If the subroutine is a function 
  1059.  or type then a value must also be returned. e.g.
  1060.  
  1061.     return
  1062.  
  1063.     return {50, "FRED", {}}
  1064.  
  1065.  An exit statement may appear inside a while loop or a for loop. It causes 
  1066.  immediate termination of the loop, with control passing to the first statement
  1067.  after the loop. e.g.
  1068.  
  1069.     for i = 1 to 100 do
  1070.         if a[i] = x then
  1071.         location = i
  1072.         exit
  1073.         end if
  1074.     end for
  1075.  
  1076.  It is also quite common to see something like this:
  1077.  
  1078.     while TRUE do
  1079.          ...
  1080.          if some_condition then
  1081.             exit
  1082.          end if
  1083.          ...
  1084.     end while
  1085.  
  1086.  i.e. an "infinite" while loop that actually terminates via an exit statement 
  1087.  at some arbitrary point in the body of the loop.
  1088.  
  1089.  
  1090.  2.5 Top-Level Commands
  1091.  ----------------------
  1092.  
  1093.  Euphoria processes your .ex file in one pass, starting at the first line and 
  1094.  proceeding through to the last line. When a procedure or function definition 
  1095.  is encountered, the routine is checked for syntax and converted into an 
  1096.  internal form, but no execution takes place. When a statement that is outside 
  1097.  of any routine is encountered, it is checked for syntax, converted into an 
  1098.  internal form and then immediately executed.  If your .ex file contains only 
  1099.  routine definitions, but no immediate execution statements, then nothing will 
  1100.  happen when you try to run it (other than syntax checking). You need to have 
  1101.  an immediate statement to call your main routine (see the example program in 
  1102.  section 1.1). It is quite possible to have a .ex file with nothing but 
  1103.  immediate statements, for example you might want to use Euphoria as a 
  1104.  desk calculator, typing in just one print (or ? see below) statement into a 
  1105.  file, and then executing it. The langwar demo program 
  1106.  (euphoria\demo\langwar\lw.ex) quickly reads in and displays a file on the 
  1107.  screen, before the rest of the program is compiled (on a 486 this makes 
  1108.  little difference as the compiler takes less than a second to finish 
  1109.  compiling the whole program). Another common practice is to immediately 
  1110.  initialize a global variable, just after its declaration.
  1111.  
  1112.  The following special commands may only appear at the top level i.e. 
  1113.  outside of any function or procedure. As we have seen, it is also 
  1114.  possible to use any Euphoria statement, including for loops, while loops, 
  1115.  if statements etc. (but not return), at the top level.
  1116.  
  1117.  include filename - reads in (compiles) a Euphoria source file in the presence 
  1118.            of any global symbols that have already been defined. 
  1119.            Global symbols defined in the included file remain visible
  1120.            in the remainder of the program. If an absolute pathname 
  1121.            is given, Euphoria will use it. When a relative pathname 
  1122.            is given, Euphoria will first look for filename in the 
  1123.            same directory as the main file given on the ex command 
  1124.            line. If it's not there, it will look in %EUDIR%\include, 
  1125.            where EUDIR is the environment variable that must be set 
  1126.            when using Euphoria. This directory contains the standard
  1127.            Euphoria include files.
  1128.  
  1129.  profile         - outputs an execution profile showing the number of times
  1130.            each statement was executed. Only statements compiled  
  1131.            with profile will be shown. The output is placed in the 
  1132.            file ex.pro. View this file with the Euphoria editor to 
  1133.            see a color display.
  1134.  
  1135.  with            - turns on one of the compile options: profile, trace, 
  1136.            warning or type_check. Options warning and type_check are
  1137.            initially on, while profile and trace are initially off. 
  1138.            These are global options. For example if you have:
  1139.  
  1140.             without type_check
  1141.             include graphics.e 
  1142.  
  1143.            then type checking will be turned off inside graphics.e as 
  1144.            well as in the current file. 
  1145.  
  1146.  without         - turns off one of the above options. Note that each of 
  1147.            these options may be turned on or off between subroutines 
  1148.            but not inside of a subroutine.
  1149.  
  1150.  
  1151.  Redirecting Standard Input and Standard Output
  1152.  ----------------------------------------------
  1153.  Routines such as gets() and puts() can use standard input (file #0), 
  1154.  standard output (file #1), and standard error output (file #2). Standard 
  1155.  input and output can then be redirected as in:
  1156.  
  1157.     ex myprog < myinput  > myoutput
  1158.  
  1159.  See section 4.5 for more details.
  1160.  
  1161.  
  1162.  
  1163.             3. Debugging
  1164.             ============
  1165.  
  1166.  
  1167.  Debugging in Euphoria is much easier than in most other programming languages.
  1168.  The extensive runtime checking provided at all times by Euphoria automatically
  1169.  catches many bugs that in other languages might take hours of your time to 
  1170.  track down. When Euphoria catches an error, you will always get a brief 
  1171.  report on your screen, and a detailed report in a file called "ex.err". 
  1172.  These reports always include a full English description of what happened, 
  1173.  along with a call-stack traceback. The file ex.err will also have a dump of 
  1174.  all variable values, and optionally a list of the most recently executed 
  1175.  statements. For extremely large sequences, only a partial dump is shown.
  1176.  
  1177.  In addition, you are able to create user-defined types that precisely 
  1178.  determine the set of legal values for each of your variables. An error 
  1179.  report will occur the moment that one your variables is assigned an illegal 
  1180.  value.
  1181.  
  1182.  Sometimes a program will misbehave without failing any runtime checks. In 
  1183.  any programming language it may be a good idea to simply study the source 
  1184.  code and rethink the algorithm that you have coded. It may also be useful 
  1185.  to insert print statements at strategic locations in order to monitor the 
  1186.  internal logic of the program. This approach is particularly convenient in 
  1187.  an interpreted language like Euphoria since you can simply edit the source
  1188.  and rerun the program without waiting for a recompile/relink.  
  1189.  
  1190.  Euphoria provides you with additional powerful tools for debugging. You 
  1191.  can trace the execution of your program source code on one screen while 
  1192.  you witness the output of your program on another. with trace / without trace 
  1193.  commands select the subroutines in your program that are available for tracing.
  1194.  Often you will simply insert a  with trace  command at the very beginning of 
  1195.  your source code to make it all traceable. Sometimes it is better to place 
  1196.  the first  with trace  after all of your user-defined types, so you don't 
  1197.  trace into these routines after each assignment to a variable. At other times,
  1198.  you may know exactly which routine or routines you are interested in tracing, 
  1199.  and you will want to select only these ones. Of course, once you are in the 
  1200.  trace window you can interactively skip over the execution of any routine by 
  1201.  pressing down-arrow on the keyboard rather than Enter. 
  1202.  
  1203.  Only traceable lines can appear in ex.err as "most-recently-executed lines" 
  1204.  should a runtime error occur. If you want this information and didn't get it, 
  1205.  you should insert a with trace and then rerun your program. Execution will 
  1206.  be a bit slower when lines compiled with trace are executed.
  1207.  
  1208.  After you have predetermined the lines that are traceable, your program must 
  1209.  then dynamically cause the trace facility to be activated by executing a 
  1210.  trace(1) statement. Again, you could simply say:
  1211.  
  1212.     with trace
  1213.     trace(1)
  1214.  
  1215.  at the top of your program, so you can start tracing from the beginning of 
  1216.  execution. More commonly, you will want to trigger tracing when a certain 
  1217.  routine is entered, or when some condition arises. e.g.
  1218.     
  1219.     if  x < 0 then
  1220.         trace(1)
  1221.     end if
  1222.  
  1223.  You can turn off tracing by executing a trace(0) statement. You can also 
  1224.  turn it off interactively by typing 'q' to quit tracing. Remember that 
  1225.  with trace  must appear outside of any routine, whereas trace(1) and 
  1226.  trace(0) can appear inside a routine or outside.
  1227.  
  1228.  You might want to turn on tracing from within a type. Suppose you run 
  1229.  your program and it fails, with the ex.err file showing that one of your 
  1230.  variables has been set to a strange, although not illegal value, and you 
  1231.  wonder how it could have happened. Simply create a type for that variable 
  1232.  that executes trace(1) if the value being assigned to the variable is the 
  1233.  strange one that you are interested in.
  1234.  e.g.
  1235.     type positive_int(integer x)
  1236.         if x = 99 then
  1237.         trace(1) -- how can this be???
  1238.         return 1 -- keep going
  1239.         else
  1240.         return x > 0
  1241.         end if
  1242.     end type
  1243.  
  1244.  You will then be able to see the exact statement that caused your variable 
  1245.  to be set to the strange value, and you will be able to check the values 
  1246.  of other variables. You will also be able to check the output screen to 
  1247.  see what has been happening up to this precise moment. If you make your 
  1248.  special type return 0 for the strange value instead of 1, you can force a 
  1249.  dump into ex.err.
  1250.  
  1251.  The Trace Screen
  1252.  ----------------
  1253.  When a trace(1) statement is executed, your main output screen is saved and 
  1254.  a trace screen appears. It shows a view of your program with the statement 
  1255.  that will be executed next highlighted, and several statements before and 
  1256.  after showing as well. Several lines at the bottom of the screen are 
  1257.  reserved for displaying variable names and values. The top line shows the 
  1258.  commands that you can enter at this point:
  1259.  
  1260.  F1 - display main output screen - take a look at your program's output so far
  1261.  
  1262.  F2 - redisplay trace screen. Press this key while viewing the main output 
  1263.       screen to return to the trace display.
  1264.  
  1265.  Enter - execute the currently-highlighted statement only
  1266.  
  1267.  down-arrow - continue execution and break when any statement coming after 
  1268.           this one in the source listing is executed. This lets you skip 
  1269.           over subroutine calls. It also lets you force your way out of 
  1270.           repetitive loops.
  1271.  
  1272.  ? - display the value of a variable. Many variables are displayed 
  1273.      automatically as they are assigned a value, but sometimes you will have 
  1274.      to explicitly ask for one that is not on display. After hitting ?
  1275.      you will be prompted for the name of the variable. Variables that are 
  1276.      not defined at this point cannot be shown. Variables that have not yet 
  1277.      been initialized will have <NO VALUE> beside their name.
  1278.  
  1279.  q - quit tracing and resume normal execution. Tracing will start again when 
  1280.      the next trace(1) is executed.
  1281.  
  1282.  ! - this will abort execution of your program. A traceback and dump of 
  1283.      variable values will go to ex.err.
  1284.  
  1285.  As you trace your program, variable names and values appear automatically in 
  1286.  the bottom portion of the screen. Whenever a variable is assigned-to you will 
  1287.  see its name and new value appear at the bottom. This value is always kept 
  1288.  up-to-date. Private variables are automatically cleared from the screen 
  1289.  when their routine returns. When the variable display area is full, 
  1290.  least-recently referenced variables will be discarded to make room for 
  1291.  new variables. 
  1292.  
  1293.  The trace screen adopts the same graphics mode as the main output screen. 
  1294.  This makes flipping between them quicker and easier.
  1295.  
  1296.  When a traced program requests keyboard input, the main output screen will 
  1297.  appear, to let you type your input as you normally would. This works fine for 
  1298.  gets() input. When get_key() (quickly samples the keyboard) is called you 
  1299.  will be given 10 seconds to type a character otherwise it is assumed that 
  1300.  there is no input for this call to get_key(). This allows you to test the 
  1301.  case of input and also the case of no input for get_key().
  1302.  
  1303.  
  1304.  
  1305.  
  1306.             4. Built-in Routines
  1307.             ====================
  1308.  
  1309.  
  1310.  Many built-in procedures and functions are provided. The names of these 
  1311.  routines are not reserved. If a user-defined symbol has the same name, it 
  1312.  will override the built-in routine until the end of scope of the 
  1313.  user-defined symbol (you will get a suppressible warning about this). Some 
  1314.  routines are written in Euphoria and you must include one of the .e files in 
  1315.  euphoria\include to use them. Where this is the case, the appropriate include 
  1316.  file is noted. The editor displays in magenta those routines that are part 
  1317.  of the interpreter, ex.exe, and require no include file.
  1318.  
  1319.  To indicate what kind of object may be passed in and returned, the following 
  1320.  prefixes are used:
  1321.  
  1322.    x - a general object (atom or sequence)
  1323.    s - a sequence
  1324.    a - an atom
  1325.    i - an integer 
  1326.   fn - an integer used as a file number
  1327.   st - a string sequence, or single-character atom
  1328.  
  1329.  An error will result if an illegal argument value is passed to any of these 
  1330.  routines.
  1331.  
  1332.  
  1333.  4.1 Predefined Types
  1334.  --------------------
  1335.  
  1336.  As well as declaring variables with these types, you can also call them 
  1337.  just like ordinary functions, in order to test if a value is a certain type.
  1338.  
  1339.  
  1340.  i = integer(x)
  1341.     Return 1 if x is an integer in the range -1073741824 to +1073741823. 
  1342.     Otherwise return 0.  
  1343.  
  1344.  
  1345.  i = atom(x)
  1346.     Return 1 if x is an atom else return 0.
  1347.  
  1348.  
  1349.  i = sequence(x)
  1350.     Return 1 if x is a sequence else return 0.
  1351.  
  1352.  
  1353.  4.2 Sequence Manipulating Routines
  1354.  ----------------------------------
  1355.  
  1356.  i = length(s)
  1357.     Return the length of s. s must be a sequence. e.g.
  1358.  
  1359.         length({{1,2}, {3,4}, {5,6}}) -- 3
  1360.         length("")              -- 0
  1361.         length({})              -- 0
  1362.  
  1363.     Notice that {} and "" both represent the empty sequence.
  1364.     As a matter of style, use "" when you are thinking of it as an 
  1365.     empty string of characters. Use {} when it is an empty sequence
  1366.     in general.
  1367.  
  1368.  
  1369.  s = repeat(x, a)
  1370.     Create a sequence of length a where each element is x.
  1371.     e.g.
  1372.         repeat(0, 10)      -- {0,0,0,0,0,0,0,0,0,0}
  1373.         repeat("JOHN", 4)  -- {"JOHN", "JOHN", "JOHN", "JOHN"}
  1374.  
  1375.  
  1376.  s2 = append(s1, x)
  1377.     Append x to the end of sequence s1. The length of s2 will be 
  1378.     length(s1) + 1. If x is an atom this is the same as 
  1379.     s2 = s1 & x. If x is a sequence it is definitely not the same. 
  1380.     You can use append to dynamically grow a sequence e.g.
  1381.     
  1382.         x = {}
  1383.         for i = 1 to 10 do
  1384.             x = append(x, i)
  1385.         end for
  1386.         -- x is now {1,2,3,4,5,6,7,8,9,10}
  1387.  
  1388.     The necessary storage is allocated automatically (and quite 
  1389.     efficiently) with Euphoria's dynamic storage allocation. See also 
  1390.     the gets() example in section 4.5.
  1391.  
  1392.  
  1393.  s2 = prepend(s1, x)
  1394.     Prepend x to the start of sequence s1. The length of s2 will be 
  1395.     length(s1) + 1. If x is an atom this is the same as s2 = x & s1. 
  1396.     If x is a sequence it is definitely not the same. e.g.
  1397.     
  1398.         prepend({1,2,3}, {0,0})    -- {{0,0}, 1, 2, 3}
  1399.         {0,0} & {1,2,3}        -- {0, 0, 1, 2, 3}
  1400.  
  1401.  
  1402.  4.3 Searching and Sorting 
  1403.  ------------------------- 
  1404.  
  1405.  i = compare(x1, x2)
  1406.     Return 0 if objects x1 and x2 are identical, 1 if x1 is greater 
  1407.     than x2, -1 if x1 is less than x2. Atoms are considered to be less 
  1408.     than sequences. Sequences are compared "alphabetically" starting 
  1409.     with the first element until a difference is found. e.g.
  1410.  
  1411.         compare("ABC", "ABCD")    -- -1
  1412.  
  1413.  
  1414.  i = find(x, s)
  1415.     Find x as an element of s. If successful, return the first element 
  1416.     number of s where there is a match. If unsuccessful return 0. 
  1417.     Example:
  1418.  
  1419.         location = find(11, {5, 8, 11, 2, 3})
  1420.         -- location is set to 3
  1421.  
  1422.         names = {"fred", "rob", "george", "mary", ""}
  1423.         location = find("mary", names)
  1424.         -- location is set to 4
  1425.  
  1426.  
  1427.  i = match(s1, s2)
  1428.     Try to match s1 against some slice of s2. If successful, return 
  1429.     the element number of s2 where the (first) matching slice begins, 
  1430.     else return 0. Example:
  1431.  
  1432.         location = match("pho", "Euphoria")
  1433.         -- location is set to 3
  1434.  
  1435.  
  1436.  include sort.e
  1437.  x2 = sort(x1)
  1438.     Sort x into ascending order using a fast sorting algorithm. By 
  1439.     defining your own compare function to override the built-in 
  1440.     compare(), you can change the ordering of values from sort(), and 
  1441.     perhaps choose a field or element number on which to base the sort.
  1442.     Define your compare() as a global function before including sort.e.
  1443.     Example:
  1444.         x = 0 & sort({7,5,3,8}) & 0
  1445.         -- x is set to {0, 3, 5, 7, 8, 0}
  1446.  
  1447.  
  1448.  4.4 Math
  1449.  --------
  1450.  
  1451.  These routines can be applied to individual atoms or to sequences of values. 
  1452.  See "Arithmetic Operations on Sequences" in chapter 2.
  1453.  
  1454.  x2 = sqrt(x1)   
  1455.     Calculate the square root of x1.
  1456.  
  1457.  x2 = rand(x1) 
  1458.     Return a random integer from 1 to x1 where x1 is from 1 to 32767.
  1459.  
  1460.  x2 = sin(x1)
  1461.     Return the sin of x1, where x1 is in radians.
  1462.  
  1463.  x2 = cos(x1)
  1464.     Return the cos of x1, where x1 is in radians.
  1465.  
  1466.  x2 = tan(x1) 
  1467.     Return the tan of x1, where x1 is in radians.
  1468.  
  1469.  x2 = log(x1)
  1470.     Return the natural log of x1
  1471.  
  1472.  x2 = floor(x1)
  1473.     Return the greatest integer less than or equal to x1.
  1474.  
  1475.  x3 = remainder(x1, x2)
  1476.     Compute the remainder after dividing x1 by x2. The result will have 
  1477.     the same sign as x1, and the magnitude of the result will be less 
  1478.     than the magnitude of x2.
  1479.  
  1480.  x3 = power(x1, x2)
  1481.     Raise x1 to the power x2
  1482.  
  1483.  Examples:
  1484.     sin_x = sin({.5, .9, .11})         -- {.479, .783, .110} 
  1485.  
  1486.      ? power({5, 4, 3.5}, {2, 1, -0.5})  -- {25, 4, 0.534522}
  1487.  
  1488.      ? remainder({81, -3.5, -9, 5.5}, {8, -1.7, 2, -4})
  1489.                          -- {1, -0.1, -1, 1.5}
  1490.  
  1491.  
  1492.  4.5 File and Device I/O
  1493.  -----------------------
  1494.  
  1495.  To do input or output on a file or device you must first open the file or 
  1496.  device, then use the routines below to read or write to it, then close 
  1497.  the file or device. open() will give you a file number to use as the first 
  1498.  argument of the other I/O routines. Certain files/devices are opened for you 
  1499.  automatically:
  1500.  
  1501.          0 - standard input
  1502.          1 - standard output
  1503.          2 - standard error
  1504.  
  1505.  Unless you redirect them on the command line, standard input comes from 
  1506.  the keyboard, standard output and standard error go to the screen. When 
  1507.  you write something to the screen it is written immediately without 
  1508.  buffering. If you write to a file, your characters are put into a buffer 
  1509.  until there are enough of them to write out efficiently. When you close 
  1510.  the file or device, any remaining characters are written out. When your 
  1511.  program terminates, any files that are still open will be closed for you 
  1512.  automatically.
  1513.  
  1514.  fn = open(s1, s2)
  1515.     Open a file or device, to get the file number. -1 is returned if the 
  1516.     open fails. s1 is the path name of the file or device.  s2 is the 
  1517.     mode in which the file is to be opened. Possible modes are:
  1518.       "r"  - open text file for reading
  1519.       "rb" - open binary file for reading
  1520.       "w"  - create text file for writing
  1521.       "wb" - create binary file for writing
  1522.       "u"  - open text file for update (reading and writing)
  1523.       "ub" - open binary file for update
  1524.       "a"  - open text file for appending
  1525.       "ab" - open binary file for appending
  1526.  
  1527.     Files opened for read or update must already exist. Files opened
  1528.     for write or append will be created if necessary. A file opened 
  1529.     for write will be set to 0 bytes. Output to a file opened for
  1530.     append will start at the end of file.
  1531.  
  1532.     Output to text files will have carriage-return characters 
  1533.     automatically added before linefeed characters. On input, these 
  1534.     carriage-return characters are removed. Null characters (0) are 
  1535.     removed from output. I/O to binary files is not modified in any way.
  1536.  
  1537.     Some typical devices that you can open are:
  1538.       "CON"    the console (screen)
  1539.       "AUX"    the serial auxiliary port 
  1540.       "COM1"   serial port 1
  1541.       "COM2"   serial port 2
  1542.       "PRN"    the printer on the parallel port
  1543.       "NUL"    a non-existent device that accepts and discards output 
  1544.  
  1545.  
  1546.  close(fn)
  1547.     Close a file or device and flush out any still-buffered characters.
  1548.  
  1549.  
  1550.  print(fn, x)
  1551.     Print an object x with braces { , , , } to show the structure.
  1552.     If you want to see a string of characters, rather than just the 
  1553.     ASCII codes, you need to use puts or printf below. e.g.
  1554.  
  1555.         print(1, "ABC")  -- output is:  {65, 66, 67}
  1556.          puts(1, "ABC")  -- output is:  ABC
  1557.  
  1558.  ? x    This is just a shorthand way of saying print(1, x), i.e. printing
  1559.     the value of an expression to the standard output. For example 
  1560.     ? {1, 2} + {3, 4}  would display {4, 6}. ? adds new-lines to 
  1561.     make the output more readable on your screen (or standard output).  
  1562.  
  1563.  
  1564.  printf(fn, st, x)
  1565.     Print x using format string st. If x is an atom then a single 
  1566.     value will be printed. If x is a sequence, then formats from st 
  1567.     are applied to successive elements of x. Thus printf always takes 
  1568.     exactly 3 arguments. Only the length of the last argument, 
  1569.     containing the values to be printed, will vary. The basic formats 
  1570.     are: 
  1571.       %d - print an atom as a decimal integer
  1572.       %x - print an atom as a hexadecimal integer
  1573.       %o - print an atom as an octal integer
  1574.       %s - print a sequence as a string of characters
  1575.       %e - print an atom as a floating point number with exponential 
  1576.            notation
  1577.       %f - print an atom as a floating-point number with a decimal point
  1578.            but no exponent
  1579.       %g - print an atom as a floating point number using either
  1580.          the %f or %e format, whichever seems more appropriate
  1581.       %% - print '%' character
  1582.  
  1583.     Field widths can be added to the basic formats, e.g. %5d, or %8.2f. 
  1584.     The number before the decimal point is the minimum field width to be 
  1585.     used. The number after the decimal point is the precision to be used.
  1586.  
  1587.     If the field width is negative, e.g. %-5d then the value will be 
  1588.     left-justified within the field. Normally it will be right-justified.
  1589.     If the field width starts with a leading 0 e.g. %08d then leading
  1590.     zeros will be supplied to fill up the field. If the field width
  1591.     starts with a '+' e.g. %+7d then a plus sign will be printed for
  1592.     positive values. 
  1593.  
  1594.     Examples:
  1595.          rate = 7.75
  1596.          printf(myfile, "The interest rate is: %8.2f\n", rate)
  1597.  
  1598.          name="John Smith"
  1599.          score=97
  1600.          printf(1, "%15s, %5d\n", {name, score})
  1601.  
  1602.     Watch out for the following common mistake:
  1603.  
  1604.          printf(1, "%15s", name)
  1605.  
  1606.     This will print only the first character of name, as each element 
  1607.     of name is taken to be a separate value to be formatted. You must 
  1608.     say this instead:
  1609.  
  1610.          printf(1, "%15s", {name})
  1611.          
  1612.  
  1613.  puts(fn, x)
  1614.     Print a single character (atom) or sequence of characters as bytes 
  1615.     of text. e.g.
  1616.  
  1617.     puts(screen, "Enter your first name: ")
  1618.  
  1619.     puts(output, 'A')  -- the single byte 65 will be sent to output                 
  1620.  
  1621.  
  1622.  i = getc(fn)
  1623.     Get the next character (byte) from file fn.  -1 is returned at end 
  1624.     of file
  1625.  
  1626.  
  1627.  x = gets(fn)
  1628.     Get a sequence (one line, including \n) of characters from text 
  1629.     file fn. The atom -1 is returned on end of file. Example:
  1630.  
  1631.         -- read a text file into a sequence
  1632.         buffer = {}
  1633.         while 1 do
  1634.                  line = gets(0)
  1635.                  if atom(line) then
  1636.                 exit   -- end of file
  1637.                  end if
  1638.                  buffer = append(buffer, line)
  1639.            end while
  1640.  
  1641.  
  1642.  i = get_key()
  1643.     Return the key that was pressed by the user, without waiting for 
  1644.     carriage return, or return -1 if no key was pressed
  1645.  
  1646.  
  1647.  include get.e
  1648.  s = get(fn)
  1649.     Read the next representation of a Euphoria object from file fn, and 
  1650.      convert it into the value of that object. s will be a 2-element 
  1651.     sequence {error status, value}. Error status values are:
  1652.  
  1653.         GET_SUCCESS  -- object was read successfully
  1654.         GET_EOF      -- end of file before object was read
  1655.         GET_FAIL     -- object is not syntactically correct
  1656.  
  1657.     As a simple example, suppose your program asks the user to enter 
  1658.     a number from the keyboard. If he types 77.5, get(0) would return 
  1659.     {GET_SUCCESS, 77.5} whereas gets(0) would return "77.5\n". 
  1660.  
  1661.     get() can read arbitrarily complicated Euphoria objects. You could 
  1662.     have a long sequence of values in braces and separated by commas, 
  1663.     e.g. {23, {49, 57}, 0.5, -1, 99, 'A', "john"}. A single call to 
  1664.     get() will read in this entire sequence and return it's value as 
  1665.     a result. The combination of print() and get() can be used to 
  1666.     save any Euphoria object to disk and later read it back. This 
  1667.     technique could be used to implement a database as one or more 
  1668.     large Euphoria sequences stored in disk files. The sequences 
  1669.     could be read into memory, updated and then written back to disk
  1670.     after each series of transactions is complete. See demo\mydata.ex. 
  1671.  
  1672.     Each call to get() picks up where the previous call left off. For 
  1673.     instance, a series of 5 calls to get() would be needed to read in: 
  1674.     99 5.2 {1,2,3} "Hello" -1.
  1675.  
  1676.  include file.e for the following:
  1677.  i1 = seek(fn, i2)
  1678.     Seek (move) to any byte position in the file fn or to the end of file
  1679.     if i2 is -1. For each open file there is a current byte position
  1680.     that is updated as a result of I/O operations on the file. The 
  1681.     initial file position is 0 for files opened for read, write or update.
  1682.     The initial position is the end of file for files opened for append. 
  1683.     The value returned by seek() is 0 if the seek was successful, and 
  1684.     non-zero if it was unsuccessful. It is possible to seek past the
  1685.     end of a file. In this case undefined bytes will be added to the file
  1686.     to make it long enough for the seek.
  1687.     
  1688.  i = where(fn)
  1689.     This function returns the current byte position in the file fn.
  1690.     This position is updated by reads, writes and seeks on the file.
  1691.     It is the place in the file where the next byte will be read from, 
  1692.     or written to.
  1693.  
  1694.  s = current_dir()
  1695.     Return the name of the current working directory.
  1696.  
  1697.  x = dir(st)
  1698.     Return directory information for the file or directory named by st.
  1699.     If there is no file or directory with this name then -1 is returned.
  1700.     This information is similar to what you would get from the DOS dir
  1701.     command. A sequence is returned where each element is a sequence 
  1702.     that describes one file or subdirectory. For example:
  1703.     {
  1704.        {".",    "d",     0  1994, 1, 18,  9, 30, 02},
  1705.        {"..",   "d",     0  1994, 1, 18,  9, 20, 14},
  1706.        {"fred", "ra", 2350, 1994, 1, 22, 17, 22, 40},
  1707.        {"sub",  "d" ,    0, 1993, 9, 20,  8, 50, 12}
  1708.     } 
  1709.     If st names a directory you will have entries for "." and "..", just 
  1710.     as with the DOS dir command. If st names a file then x will have just 
  1711.     one entry, i.e. length(x) will be 1. Each entry contains the name, 
  1712.     attributes and file size as well as the year, month, day, hour, minute
  1713.     and second of the last modification. The attributes element is a 
  1714.     string sequence containing characters chosen from:
  1715.  
  1716.         d - directory
  1717.         r - read only file
  1718.         h - hidden file
  1719.         s - system file
  1720.         v - volume-id entry
  1721.         a - archive file
  1722.  
  1723.     A normal file without special attributes would just have an empty
  1724.     string, "", in this field. See bin\walkdir.ex for an example that 
  1725.     uses the dir() function.
  1726.         
  1727.  
  1728.  4.6 Mouse Support
  1729.  -----------------
  1730.  
  1731.  include mouse.e  -- required for these routines
  1732.  
  1733.  x = get_mouse()
  1734.     Return the last mouse event in the form {event, x, y}, or return -1 
  1735.     if there has not been a mouse event since the last time get_mouse() 
  1736.     was called. 
  1737.  
  1738.     Constants have been defined in mouse.e for the possible mouse events. 
  1739.     x and y are the coordinates of the mouse pointer at the time that 
  1740.     the event occurred. e.g. {2, 100, 50} would indicate that the 
  1741.     left button was pressed down while the mouse pointer was at position 
  1742.     x=100, y=50 on the screen. get_mouse() returns immediately with 
  1743.     either a -1 or a mouse event. It does not wait for an event to occur.
  1744.     You must check it frequently enough to avoid missing an event. When 
  1745.     the next event occurs, the current event will be lost, if you haven't 
  1746.     read it. In practice it is not hard to catch almost all events, and
  1747.     the ones that are lost are usually lost at a lower level in the 
  1748.     system, beyond the control of your program. Losing a MOVE event is 
  1749.     generally not too serious, as the next MOVE will tell you where the 
  1750.     mouse pointer is. 
  1751.  
  1752.     You can use get_mouse() in most text and graphics modes. (The SVGA 
  1753.     modes may not work fully under MS-DOS).
  1754.  
  1755.  
  1756.  mouse_events(i) 
  1757.     Use this procedure to select the mouse events that you want 
  1758.     get_mouse() to report. For example, mouse_events(LEFT_DOWN+LEFT_UP+
  1759.     RIGHT_DOWN) will restrict get_mouse() to reporting the left button 
  1760.     being pressed down or released, and the right button being pressed 
  1761.     down. All other events will be ignored. By default, get_mouse()
  1762.     will report all events. It is good practice to ignore events that 
  1763.     you are not interested in, particularly the very frequent MOVE event, 
  1764.     in order to reduce the chance that you will miss a significant event.
  1765.     mouse_events() can be called at various stages of the execution of 
  1766.     your program, as the need to detect events changes.
  1767.  
  1768.  
  1769.  mouse_pointer(i)
  1770.     If i is 0 hide the mouse pointer, otherwise turn on the mouse pointer.
  1771.     It may be necessary to hide the mouse pointer temporarily when you
  1772.     update the screen. Multiple calls to hide the pointer will require
  1773.     multiple calls to turn it back on. The first call to either get_mouse()
  1774.     or mouse_events() above, will also turn the pointer on (once).
  1775.  
  1776.  
  1777.  4.7 Operating System 
  1778.  -------------------- 
  1779.  
  1780.  a = time()
  1781.     Return the number of seconds since some fixed point in the past. The 
  1782.     resolution on MS-DOS is about 0.05 seconds. 
  1783.  
  1784.  
  1785.  s = date()
  1786.     Return a sequence with the following information:
  1787.          { year (since 1900),
  1788.            month (January = 1),
  1789.            day (day of month, starting at 1),
  1790.            hour (0 to 23),
  1791.            minute (0 to 59),
  1792.            second (0 to 59),
  1793.            day of the week (Sunday = 1),
  1794.            day of the year (January 1st = 1) }
  1795.  
  1796.  
  1797.  s = command_line() 
  1798.     Return a sequence of strings, where each string is a word from the 
  1799.     ex command line that started Euphoria. The first word will be the 
  1800.     path to the Euphoria executable. The next word is the name of 
  1801.     your Euphoria .ex file. After that will come any extra words typed 
  1802.     by the user. You can use these words in your program. Euphoria 
  1803.     does not have any command line options.
  1804.  
  1805.  
  1806.  x = getenv(s)
  1807.     Return the value of an environment variable. If the variable is 
  1808.     undefined return -1. e.g.
  1809.  
  1810.         getenv("EUDIR")  -- returns "C:\EUPHORIA" -- or D: etc.
  1811.  
  1812.  
  1813.  system(s, a)
  1814.     Pass a command string s to the operating system command interpreter
  1815.     for execution. The argument a indicates the manner in which to 
  1816.     return from the system call. 
  1817.         value of a      return action
  1818.              0      - restore previous graphics mode
  1819.                   (clears the screen)
  1820.              1      - make a beep sound, wait for a
  1821.                   key press, then restore the
  1822.                   graphics mode
  1823.              2      - do not restore graphics mode
  1824.  
  1825.     Action 2 should only be used when it is known that the system call 
  1826.     will not change the graphics mode. 
  1827.  
  1828.  
  1829.  abort(a)       
  1830.     Abort execution of the program. The argument a is an integer status 
  1831.     value to be returned to the operating system. A value of 0 indicates
  1832.     successful completion of the program. Other values can indicate 
  1833.     various kinds of errors. Euphoria for MS-DOS currently ignores this
  1834.     argument and always returns 0. abort() is useful when a program is 
  1835.     many levels deep in subroutine calls, and execution must end 
  1836.     immediately, perhaps due to a severe error that has been detected.
  1837.  
  1838.  
  1839.  Machine Dependent Routines
  1840.  --------------------------
  1841.  x = machine_func(a, x)
  1842.  machine_proc(a, x) 
  1843.     These routines perform machine-specific operations such as graphics
  1844.     and sound effects. They are meant to be called indirectly via one of
  1845.     the support routines, written in Euphoria. A direct call can cause 
  1846.     a machine exception if done incorrectly. Calls to these routines 
  1847.     may not be portable across all machines and operating systems that
  1848.     Euphoria is implemented on. 
  1849.  
  1850.  
  1851.  
  1852.  4.8 Debugging
  1853.  -------------
  1854.  
  1855.  trace(x) 
  1856.     If x is 1 then turn on full-screen statement tracing. If x is 0 then
  1857.     turn off tracing. Tracing only occurs in subroutines that were 
  1858.     compiled with trace. See the section on Debugging.
  1859.  
  1860.  
  1861.  
  1862.  4.9 Graphics & Sound  
  1863.  --------------------
  1864.  
  1865.  clear_screen()
  1866.     Clear the screen using the current background color. 
  1867.  
  1868.  
  1869.  position(a1, a2)
  1870.     Set the cursor to line a1, column a2, where the top left corner 
  1871.     is position(1,1). 
  1872.  
  1873.  
  1874.  include graphics.e    -- for the following routines:
  1875.  
  1876.  i1 = graphics_mode(i2)
  1877.     Select graphics mode i2. See graphics.e for a list of valid 
  1878.     graphics modes. If successful, i1 is set to 0, otherwise i1
  1879.     is set to 1.
  1880.  
  1881.  
  1882.  s = video_config()
  1883.     Return a sequence of values describing the current video
  1884.     configuration:
  1885.     {color monitor?, graphics mode,  text rows, text columns,
  1886.     xpixels, ypixels, number of colors}
  1887.  
  1888.  
  1889.  scroll(i)
  1890.     Scroll the screen up (i positive) or down (i negative) by i lines.
  1891.  
  1892.  
  1893.  wrap(i)
  1894.     Allow text to wrap at right margin (i = 1) or get truncated (i = 0).
  1895.  
  1896.  
  1897.  cursor(i)
  1898.     Select a style of cursor. See graphics.e for some choices.
  1899.  
  1900.  
  1901.  text_color(i)
  1902.     Set the foreground text color. Add 16 to get blinking text
  1903.     in some modes. See graphics.e for a list of possible colors.
  1904.  
  1905.  
  1906.  bk_color(i)
  1907.     Set the background color. In graphics modes the whole screen is
  1908.     affected immediately. In text modes any new characters that you 
  1909.     print will have the new background color.
  1910.  
  1911.  
  1912.  x = palette(i, s)
  1913.     Change the color for color number i to s, where s is a sequence of 
  1914.     color intensities: {red, green, blue}. Each value in s can be from 
  1915.     0 to 63. If successful, a 3-element sequence containing the 
  1916.     previous color for i will be returned, and all pixels on the screen
  1917.     with value i will be set to the new color. If unsuccessful, the 
  1918.     atom -1 will be returned. 
  1919.  
  1920.  
  1921.  i2 = text_rows(i1)
  1922.     Set the number of lines of text on the screen to i1 if possible.
  1923.     i2 will be set to the actual new number of lines.
  1924.  
  1925.  
  1926.  pixel(i, s) 
  1927.     Set a pixel using color i at point s, where s is a 2-element screen 
  1928.     coordinate {x, y}.
  1929.  
  1930.  
  1931.  i = get_pixel(s)
  1932.     Read the color number for a pixel on the screen at point s,
  1933.     where s is a 2-element screen coordinate {x, y}. -1 is returned
  1934.     for points that are off the screen.
  1935.  
  1936.  
  1937.  draw_line(i, s)
  1938.     Draw a line connecting two or more points in s, using color i. 
  1939.     Example:
  1940.  
  1941.     draw_line(7, {{100, 100}, {200, 200}, {900, 700}}) 
  1942.  
  1943.     would connect the three points in the sequence using a thin white 
  1944.     line, i.e. a line would be drawn from {100, 100} to {200, 200} and 
  1945.     another line would be drawn from {200, 200} to {900, 700}.
  1946.  
  1947.  
  1948.  polygon(i1, i2, s)
  1949.     Draw a polygon with 3 or more vertices given in s, using a certain 
  1950.     color i1. Fill the area if i2 is 1. Don't fill if i2 is 0. Example:
  1951.  
  1952.     polygon(7, 1, {{100, 100}, {200, 200}, {900, 700}})
  1953.  
  1954.     would make a solid white triangle.
  1955.  
  1956.  
  1957.  ellipse(i1, i2, s1, s2)
  1958.     Draw an ellipse with color i1. The ellipse neatly fits inside
  1959.     the rectangle defined by diagonal points s1 {x1, y1} and s2 
  1960.     {x2, y2}. If the rectangle is a square then the ellipse will be a
  1961.     circle. Fill the ellipse when i2 is 1. Don't fill when i2 is 0. 
  1962.     Example:
  1963.  
  1964.     ellipse(5, 0, {10, 10}, {20, 20})
  1965.  
  1966.     would make a magenta colored circle just fitting inside the square
  1967.     {10, 10}, {10, 20}, {20, 20}, {20, 10}.
  1968.  
  1969.  
  1970.  sound(i)
  1971.     Turn on the PC speaker at the specified frequency. If i is 0 
  1972.     the speaker will be turned off.
  1973.  
  1974.  
  1975.  4.10 Machine Level Interface 
  1976.  ----------------------------
  1977.  
  1978.  With this low-level machine interface you can read and write to memory. You
  1979.  can also set up your own 386+ machine language routines and call them. The 
  1980.  usual guarantee that Euphoria will protect you from machine-level errors 
  1981.  does *not* apply when you use these routines. There are only some simple 
  1982.  checks to catch the use of memory addresses that are negative or zero.
  1983.  (Theoretically address 0 is acceptable, but in practice it is usually an 
  1984.  error so we catch it.)
  1985.  
  1986.  Most Euphoria programmers will never use this interface, but it is 
  1987.  important because it makes it possible to get into every nook and cranny  
  1988.  of the hardware or operating system. For those with very specialized
  1989.  applications this could be crucial. 
  1990.  
  1991.  Machine code routines can be written by hand, or taken from the disassembled
  1992.  output of a compiler for C or some other language. Remember that your
  1993.  machine code will be running in 32-bit protected mode. See demo\callmach.ex 
  1994.  for an example.
  1995.  
  1996.  i = peek(a)
  1997.     Read a single byte value in the range 0 to 255 from machine address a.
  1998.  
  1999.  poke(a, i)
  2000.     Write a single byte value, i, to memory address a. remainder(i, 256)
  2001.     is actually written.
  2002.  
  2003.     Writing to the screen memory with poke() can be much faster than using
  2004.      puts() or printf(), but the programming is more difficult and less 
  2005.     portable. In most cases the speed is not needed. For example, the 
  2006.     Euphoria editor    never uses poke().
  2007.  
  2008.  call(a)
  2009.     Call a machine language routine that starts at location a. This
  2010.     routine must execute a RET instruction #C3 to return control
  2011.     to Euphoria. The routine should save and restore any registers 
  2012.     that it uses. You can allocate a block of memory for the routine
  2013.     and then poke in the bytes of machine code. You might allocate
  2014.     other blocks of memory for data and parameters that the machine
  2015.     code can operate on. The addresses of these blocks could be poked
  2016.     into the machine code. 
  2017.  
  2018.  include machine.e
  2019.  
  2020.  a = allocate(i)
  2021.     Allocate i contiguous bytes of memory. Return the address of the
  2022.     block of memory, or return 0 if the memory can't be allocated.
  2023.  
  2024.  free(a)
  2025.     Free up a previously allocated block of memory by specifying the
  2026.     address of the start of the block, i.e. the address that was
  2027.     returned by allocate(). 
  2028.  
  2029.  s = int_to_bytes(a)
  2030.     Convert an integer into a sequence of 4 bytes. These bytes are in
  2031.     the order expected on the 386+, i.e. least significant byte first.
  2032.     You would use this routine prior to poking the bytes into memory.
  2033.  
  2034.  a = bytes_to_int(s)
  2035.     Convert a sequence of 4 bytes into an integer value. The result could
  2036.     be greater than the integer type allows, so assign it to an atom.
  2037.  
  2038.  
  2039.  
  2040.              --- THE END ---
  2041.  
  2042.